/*
 * Copyright (c) 2005 -2008 Reliable Source, Inc. and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Mozilla Public License v.1.1,
 * BSD, and Eclipse Public License v1.0 which accompanies this
 * distribution and is available at:
 *
 * 		http://www.eclipse.org/legal/epl-v10.html
 * 		http://www.opensource.org/licenses/bsd-license.php
 * 		http://www.mozilla.org/MPL/MPL-1.1.html
 *
 * Contributors:
 *     IBM Corporation	     -  http://ibm.com
 *     	- initial API and implementation
 *
 *     Reliable Source, Inc. - http://reliablesource.org
 *
 */
package org.reliablesource.client.ui.workbench.views;

import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.OpenEvent;
import org.eclipse.ui.PlatformUI;
import org.reliablesource.client.ui.workbench.WorkbenchActivator;

/**
 * 
 * OpenInDetailsViewHandler.java<br>
 * Created on: Jun 26, 2008<br>
 *
 * @author <a href="mailto:dennis.park@gmail.com">Dennis Park</a>
 *
 * @category
 * @since 1.0
 */
public class OpenInDetailsViewHandler extends AbstractHandler {

    /*
     * (non-Javadoc)
     *
     * @see org.eclipse.core.commands.IHandler#execute(org.eclipse.core.commands.ExecutionEvent)
     */
    public Object execute(ExecutionEvent event) throws ExecutionException {
        OpenEvent openEvent = (OpenEvent) event.getTrigger();
        handleSelection(openEvent.getSelection());
        return (null);
    }

    /**
     * Opens the given <code>selection</code> in an anchor. The anchor opening
     * is delegated to the <code>AnchorManager</code>.
     *
     * @param  _selection  The selection that contains the object to be opened in
     *                    the anchor.
     * @throws  ExecutionException  If an exception occurred while opening the
     *                              anchor.
     */
    public void handleSelection(final ISelection _selection)
            throws ExecutionException {
        try {
            if ((_selection == null) || _selection.isEmpty()
                    || !(_selection instanceof IStructuredSelection)) {
                throw (new ReliableWorkbenchRuntimeException("Empty Selection",
                        WorkbenchActivator.PLUGIN_ID,
                        "Empty Selection"));
            }

            //               Object inputObject = ((StructuredSelection) _selection)
            //                   .getFirstElement();

            String _viewTypeMapping = "";
            PlatformUI.getWorkbench().getActiveWorkbenchWindow()
                    .getActivePage().showView(_viewTypeMapping);

        } catch (ReliableWorkbenchRuntimeException e) {
            throw (new ExecutionException("Cannot open details", e));

        } catch (Exception e) {
            throw (new ExecutionException("Cannot open details", e));
        }
    }

}
